package cn.autumnorange.app.user.consumer.authorization.security.security.ajaxjsonlogin;

import cn.autumnorange.app.user.consumer.authorization.security.security.LoginTypeService;
import cn.autumnorange.app.user.consumer.authorization.system.LoginUserDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

@Component
public class AjaxJsonLoginTypeServiceImpl implements LoginTypeService {
  @Override
  public LoginUserDto getLoginMsg(HttpServletRequest request) {
    LoginUserDto loginUserDto = null;
    // use jackson to deserialize json
    ObjectMapper mapper = new ObjectMapper();

    try (InputStream is = request.getInputStream()) {
      loginUserDto = mapper.readValue(is, LoginUserDto.class);
    } catch (IOException e) {
      e.printStackTrace();
      loginUserDto = null;
    }
    if (loginUserDto.getUserName() == null) {
      return null;
    }
    return loginUserDto;
  }
}
